package com.qingsong.component;

import cn.hutool.core.util.ObjectUtil;
import lombok.SneakyThrows;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;

/**
 * @author qingsong
 */
@Aspect
@Component
public class ServiceAspect {

    @Around("execution(public * com.qingsong.service..*.*(..))")
    @SneakyThrows
    public Object aroundAdvice(ProceedingJoinPoint joinPoint){

        //获取方法参数
        Object[] args = joinPoint.getArgs();

        //获取类名
        String className = joinPoint.getTarget().toString();

        //获取方法名
        String methodName = joinPoint.getSignature().getName();

        //前置通知:检查参数中是否存在空值
        if(ObjectUtil.hasNull(args)){
            String msg = "业务方法" + "." +methodName + "() 中存在null值参数";
            /*String msg = "业务方法" + className + "." +methodName + "() 中存在null值参数";*/
            throw new RuntimeException(msg);
        }

        //调用目标方法
        Object returnValue = joinPoint.proceed(args);

        //后置通知":记录业务层调用日志
        //System.out.println("调用业务方法" + "." + methodName+ "()");
        System.out.println("调用业务方法" + className+ "." + methodName+ "()");

        //返回目标方法的返回值
        return returnValue;
    }
}
